package stark.app.function.wifi;

import android.net.wifi.ScanResult;
import android.text.TextUtils;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

import stark.app.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.utils.ResUtil;
import stark.common.basic.utils.WiFiUtil;

/**
 * Author：daizhongbin on 2022/4/28
 * Email：1027154338@qq.com
 * Desc:
 */
public class WifiAdapter extends StkProviderMultiAdapter<ScanResult> {

    public WifiAdapter() {
        super(1);
        addItemProvider(new WifiItemProvider());
    }

    private class WifiItemProvider extends BaseItemProvider<ScanResult> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_wifi;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, ScanResult scanResult) {
            String sidName = scanResult.SSID;
            if (TextUtils.isEmpty(sidName)) {
                sidName = "Unknown";
            }
            holder.setText(R.id.tvSid, sidName);

            boolean isOpened = WiFiUtil.isOpened(scanResult);
            boolean isConnected = WiFiUtil.isConnected(scanResult);
            boolean isConfigured = WiFiUtil.isConfigured(scanResult);

            //status
            String stateText;
            if (isConnected) {
                stateText = ResUtil.getStr(R.string.connected);
            } else if (isConfigured) {
                stateText = ResUtil.getStr(R.string.have_saved);
                if (isOpened) {
                    stateText += ", " + ResUtil.getStr(R.string.opened);
                } else {
                    stateText += ", " + ResUtil.getStr(R.string.encrypt);
                }
            } else {
                if (isOpened) {
                    stateText = ResUtil.getStr(R.string.opened);
                } else {
                    stateText = ResUtil.getStr(R.string.encrypt);
                }
            }
            holder.setText(R.id.tvStatus, stateText);

            //iv lock
            holder.setVisible(R.id.ivLock, !isOpened);


        }
    }

}
